$(function(){
	$(document).keydown(function(){ 
		if(event.keyCode==13){ 
		$("#chaxun").trigger('click'); 
		return false;
		}
	});
});
	// 获取页面参数
var	pageNumber = 1;
var	totalNumber = 0;

//科室管理的全局变量
var keshigl = {
	keshifzrs: '',
	url: {
		insertKeshiUrl: path + '/keshi/insertKeshi',
		getKeshiCountUrl: path + '/keshi/getKeshiCount',
		getKeshiByPageUrl: path + '/keshi/getKeshiByPage',
		updateKeshiByIdUrl: path + '/keshi/updateKeshiById',
		getKeshifzrUrl: path + '/keshi/getKeshifzr',
		getYuangongByKsIdUrl:path+ '/yuangong/getYuangongByKsId',
		getPTKeshiByJsmURL: path+ '/zidian/getPTKeshiByJsm',
	},
	init: function(){
		keshigl.getKeshifzr();
		keshigl.find();
		keshigl.bandFunction();
	},
	getinsertMsg: function(){
		var keshimc = $('#input-keshimc').val();
		var keshifzrid = $('#input-keshifzr').attr('keshifzrid');
		var zhuangtai1 = $('#zhuangtai1').text();
		var jinyongbz;
		switch(zhuangtai1){
		case('启用'): jinyongbz = 0;
		break;
		case('禁用'): jinyongbz = 1;
		};
		var insertMsg = {
			"keshimc": keshimc,
			"keshifzrid": keshifzrid,
			"jinyongbz": jinyongbz
		};
		return insertMsg;
	},
	getmsg: function(){
		var keshimc = $('#keshimc').val();
		var zhuangtai = $('#zhuangtai').text();
		var jinyongbz;
		switch(zhuangtai){
		case('全部'): jinyongbz = 2;
		break;
		case('启用'): jinyongbz = 0;
		break;
		case('禁用'): jinyongbz = 1;
		};
		var msg = {
			"keshimc": keshimc,
			"jinyongbz": jinyongbz
		};
		return msg;
	},
	find: function(){
		var keshimc = $('#keshimc').val();
		var zhuangtai = $('#zhuangtai').text();
		var jinyongbz;
		switch(zhuangtai){
		case('全部'): jinyongbz = "";
		break;
		case('启用'): jinyongbz = 0;
		break;
		case('禁用'): jinyongbz = 1;
		}
		$.ajax({
			url: keshigl.url.getKeshiCountUrl,
			type: 'post',
			data: keshigl.getmsg(),
			async: false,
			success: function(data){
				if(data.data != null){
					totalNumber = data.data;
				}
			},
			error: function(){
					console.log("请求失败");
			}
		});
		
		$('#table-keshigl').html('');
		var container = $('#pagination-fenykeshi');
		container.pagination({
		dataSource: keshigl.url.getKeshiByPageUrl + '?jinyongbz=' + jinyongbz + '&keshimc=' + keshimc,
		locator: 'data',
		pageSize: 12,
		totalNumber: totalNumber,
		 //增加总记录数
		showNavigator: true,
		pageNumber: pageNumber,
	    formatNavigator: '共 <%= totalNumber %> 条',
		callback: function(data, pagination){
			$("#table-keshigl").html("");
	        	  $.each(data, function(i, v){
	             	    var perPageRecordNum = pagination.pageSize; //每页数据行数
	            		var pageNo = pagination.pageNumber; //当前页号
	            		var index = (((pageNo-1) * perPageRecordNum) + i + 1);
	            		if(data[i].jinyongbz == 0){
	 						var text = '启用';
	 					}else if(data[i].jinyongbz == 1){
	 						var text = '禁用';
	 					}
	            		var content = "<tr id='keshigl" + this.id + "' value='"+this.id+"'>"
			    				+ "<td width='8%' ><label class='label'>" + (((pageNo - 1) * perPageRecordNum) + i + 1) + "</label></td>"
			     	 			+ "<td width='25%' style='overflow: visible;position: relative;'><input type='text' class='dropdown-toggle input-sm col-sm-12 u-input bzxx keshimc' name='' value='"+data[i].keshimc+"' /><ul id='' class='keshimcul dropdown-menu li-width f-input-li'  style='display:none;top: 34px; padding:0;left: 3px;border-radius: 0;width:200%;' role='menu' ></ul></td>"
			     	 			+ "<td width='25%' style='overflow: visible;position: relative;' ><input type='text'  readonly='readonly' class=' col-sm-12 dropdown-toggle u-input keshifzr' keshifzrid='" + data[i].keshifzrid + "' value='"+common_util.nullToEmpty(data[i].keshifzr)+"' style='width: 100%;'/>"
								+ '<ul class="dropdown-menu xuanzeksfzr" role="menu" style="width: 98%;top: 36px;left: 3px;border: none;padding: 0;">'
								+ keshigl.keshifzrs
	                            + "</ul>"
			     	 			+ "</td>"
			     	 			+ "<td width='15%' style='overflow: visible;'>" 
			     	 			+ '<div class="menu-up choose dropdown-search-li none-border" style="position: relative;">' 
			     	 			+ '<input type="hidden" value="0">' 
			     	 			+ '<div class="btn dropdown-toggle select-menu" data-toggle="dropdown" tabindex="0" ><span class="jinyongbz">'+text+'</span></div>' 
			     	 			+ '<ul class="dropdown-menu lbzhuangtai" role="menu" style="top:32px;left:0;"><li value="1" class="xuanzeli">禁用</li><li value="0" class="xuanzeli">启用</li></ul></div>' 
			     	 			+ "</td>" 
			             		+ '<td width="15%" ><a class="operation-left" onclick="keshigl.baocun(' + data[i].id + ')">保存</a>'
			             		+ '</td>'
			             		+ "</tr>";
	            		$("#table-keshigl").append(content);
	            		
	             });
	         }
	     });
		if(totalNumber <= 12) {
			$('#pagination-fenykeshi').css('display','none');
		}else{
			$('#pagination-fenykeshi').css('display','block');
		}
	},
	getKeshifzr: function(){
		$.ajax({
			url: keshigl.url.getKeshifzrUrl,
			type: 'post',
			success: function(data){
				var len = data.data.length;
				keshigl.keshifzrs = '';
				for(var i=0;i<len;i++){
					var str = data.data[i].split(':');
					keshigl.keshifzrs += '<li class="xialali" keshifzrid="' + str[0] + '">' + str[1] + '</li>';
				}
				$('.xuanzeksfzr').html(keshigl.keshifzrs);
			}
		});
	},
	insertKeshi: function(){
		$.ajax({
			url: keshigl.url.insertKeshiUrl,
			type: 'post',
			data: keshigl.getinsertMsg(),
			success: function(data){
				if(data.data == 'success'){
					notifySuccess("保存成功");
					keshigl.find();
					$('#input-keshimc').val('');
					$('#input-keshifzr').val('');
					$('#input-keshifzr').attr('keshifzrid','');
				}else{
					notifyWarning("保存失败");
				}
			}
		});
	},
	getPTKeshiByJsm: function (jiansuoma) {
		$.ajax({
			type: "get",
			data: {"jiansuoma": jiansuoma},
			url: keshigl.url.getPTKeshiByJsmURL,
			success: function(data) {
				var str = '';
				$.each(data.data,function () {
					str += '<li>'+ this.keshimc + '</li>';
				});
				$('#keshimcul').html(str);
			}
		});
	},
	baocun: function(id){
		var keshimc = $('#keshigl' + id).find('.keshimc').val();
		var keshifzrid = $('#keshigl' + id).find('.keshifzr').attr('keshifzrid');
		var zhuangtai = $('#keshigl' + id).find('.jinyongbz').text();
		var jinyongbz;
		switch(zhuangtai){
		case '启用': jinyongbz = 0;break;
		case '禁用': jinyongbz = 1;break;
		};
		if(keshifzrid == "null") {
			keshifzrid = null;
		}
		var msg = {
			"id": id,
			"keshimc": keshimc,
			"keshifzrid": keshifzrid,
			"jinyongbz": jinyongbz
		};
		$.ajax({
			url: keshigl.url.updateKeshiByIdUrl,
			type: 'post',
			data: msg,
			async:false,
			success: function(data){
				if(data.data == 'success'){
					notifySuccess("保存成功");
					keshigl.find();
				}else if (data.data == '请至少保留一条可用科室'){
					notifyWarning(data.data);
				} else {
					notifyWarning("保存失败");
				}
			}
		});
	},
	//绑定的方法
	bandFunction: function(){
		//绑定启用禁用按钮
		$(document).on('click','.xuanzeli',function(){
			var thisLi = $(this);
			var keshiid = $(this).parents("tr").attr("value");
			if($(this).text() == "禁用") {
				$.post(keshigl.url.getYuangongByKsIdUrl,{keshiId:keshiid},function(result){
					if(result.succsess) {
						if(result.data.length > 0) {
							notifyWarning("当前科室存在员工，不能禁用");
							thisLi.parent().css('display','none');
						}else{
							thisLi.parent().parent().find('span').text($(thisLi).text());
							thisLi.parent().css('display','none');
						}
					}
				})
			}else{
				$(this).parent().parent().find('span').text($(this).text());
				$(this).parent().css('display','none');
			}
		});
		$(document).on('click','.kusztul li',function(){
			$(this).parent().parent().find('span').text($(this).text());
			$(this).parent().css('display','none');
			keshigl.find();
		});
		// 列表状态下拉选择
		$(document).on('click','#zhuangtxaul li',function(){
			$(this).parent().parent().find('span').text($(this).text());
			$(this).parent().css('display','none');
		});
		
		//查询按钮
		$('#chaxun').click(function(){
			keshigl.find();
			return false;
		});
		
		//重置按钮
		$('#chongzhi').click(function(){
			$('#keshimc').val('');
			$('#zhuangtai').text('启用');
			keshigl.find();
			return false;
		});
		$('#reset').click(function(){
			$('#input-keshimc').val('');
			$('#input-keshifzr').val('');
			$('#zhuangtai1').text('启用');
		});
		
		//科室负责人下拉
		$(document).on('click','.xialali',function(){
			$(this).parent().parent().find('input').val($(this).text());
			$(this).parent().parent().find('input').attr('keshifzrid',$(this).attr('keshifzrid') + '');
			$(this).parent().css('display','none');
		});
		
		$(document).on('input','#input-keshimc',function(){
			keshigl.getPTKeshiByJsm($(this).val());
		});
		
		$(document).on('click','#keshimcul li',function(){
			$('#input-keshimc').val($(this).text());
			$(this).parent().css('display','none');
		});		
		//点击添加按钮
		$('#add').click(function(){
			if($('#input-keshimc').val() == ''){
				notifyWarning("科室名称不能为空");
				return;
			}
			if($('#input-keshifzr').val() == ''){
				$('#input-keshifzr').val('');
				//notifyWarning("科室负责人不能为空");
				//return;
			}
			keshigl.insertKeshi();
			keshigl.find();
		});
	}
}